package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;

    public void add(ShoppingCartDTO shoppingCartDTO, Dish dish) {
        //添加
        ShoppingCart shoppingCart = new ShoppingCart();
        String dishFlavor = shoppingCartDTO.getDishFlavor();
        BeanUtils.copyProperties(dish, shoppingCart);
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setAmount(dish.getPrice());
        shoppingCart.setNumber(1);
        System.out.println(shoppingCart);
        shoppingCartMapper.addShoppingCart(shoppingCart);
    }

    @Override
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断是新添加 还是 加1
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getById(shoppingCartDTO);
        Long dishId = shoppingCartDTO.getDishId();
        int count = 0;
        if (dishId != null) {
            Dish dish = dishMapper.findById(dishId);
            if (shoppingCarts.size() > 0) {
                for (ShoppingCart shoppingCart : shoppingCarts) {
                    if (shoppingCartDTO.getDishFlavor() == null || shoppingCart.getDishFlavor().equals(shoppingCartDTO.getDishFlavor())) {
                        shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                        shoppingCartMapper.update(shoppingCart);
                        return;
                    }
                    count++;
                }
                if (count == shoppingCarts.size()) {
                    add(shoppingCartDTO, dish);
                }
            } else {
                add(shoppingCartDTO, dish);
            }
        } else {
            Long setmealId = shoppingCartDTO.getSetmealId();
            Setmeal setmeal = setmealMapper.getById(setmealId);
            //套餐
            if (shoppingCarts.size() > 0) {
                System.out.println(shoppingCarts);
                ShoppingCart shoppingCart = shoppingCarts.get(0);
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                shoppingCartMapper.update(shoppingCart);
            } else {
                ShoppingCart shoppingCart = new ShoppingCart();
                BeanUtils.copyProperties(setmeal, shoppingCart);
                BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.addShoppingCart(shoppingCart);
            }
        }

    }

    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.list(userId);
        return list;
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    @Transactional
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        List<ShoppingCart> list = shoppingCartMapper.getById(shoppingCartDTO);
        System.out.println("list = " + list);
        for (ShoppingCart cart : list) {
            if (cart.getDishFlavor() == null || cart.getDishFlavor().equals(shoppingCartDTO.getDishFlavor())) {
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.update(shoppingCart);
                if (cart.getNumber() - 1 == 0) {
                    shoppingCartMapper.delete(cart.getId());
                }
                return;
            }

        }

    }
}
